<template>
  <div class="contentCate">
    <el-card class="box-card" v-if="status === 'index'">
      <el-row>
        <el-input
          v-model="searchParams.userId"
          placeholder="请输入用户id"
          clearable
          style="width:200px"
          size="mini"
        ></el-input>
        <el-select
          v-model="searchParams.format"
          size="mini"
          placeholder="请选择类型"
          clearable
        >
          <el-option
            v-for="item in typeData"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-select
          v-model="searchParams.type"
          size="mini"
          placeholder="请选择模式"
          clearable
        >
          <el-option
            v-for="item in formatData"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          ></el-option>
        </el-select>
        <el-date-picker
          v-model="searchParams.startTime"
          type="datetime"
          placeholder="选择开始时间"
          :picker-options="pickerOptions"
          size="mini"
        ></el-date-picker>
        <el-date-picker
          v-model="searchParams.endTime"
          type="datetime"
          placeholder="选择结束时间"
          :picker-options="pickerOptions"
          size="mini"
        ></el-date-picker>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getData"
          v-if="has('adminRechargeLogs/index')"
          >搜索</el-button
        >
        <el-button
          type="info"
          icon="el-icon-refresh"
          size="mini"
          @click="reset"
          v-if="has('adminRechargeLogs/index')"
          >重置</el-button
        >
      </el-row>
      <el-table
        :data="data.data"
        v-loading="table_loading"
        row-key="id"
        style="width: 100%;margin:10px auto"
        size="mini"
        :header-cell-style="{ background: '#f3f8fe', color: '#333' }"
      >
        <el-table-column
          fixed
          prop="id"
          label="ID"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="user_id"
          label="用户id"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="admin_username"
          label="操作人"
          align="center"
        ></el-table-column>
        <el-table-column prop="format" label="模式" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.format == 2" style="color:red">扣除</span>
            <span v-if="scope.row.format == 1" style="color:green">充值</span>
          </template>
        </el-table-column>

        <el-table-column prop="type" label="类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1">赠送金币</span>
            <span v-if="scope.row.type == 2">代充金币</span>
            <span v-if="scope.row.type == 4">钻石</span>
            <span v-if="scope.row.type == 3">优惠券</span>
            <span v-if="scope.row.type == 5">反悔卡</span>
          </template>
        </el-table-column>

        <el-table-column
          prop="value"
          label="数量"
          align="center"
        ></el-table-column>

        <el-table-column
          prop="created_at"
          label="操作时间"
          align="center"
        ></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              type="danger"
              size="mini"
              @click="cancel(scope.row.id,2)"
              v-if="has('adminRechargeLogs/index') && scope.row.type == 1 && scope.row.format == 1">
              代充
            </el-button>
            <el-button
              type="danger"
              size="mini"
              @click="cancel(scope.row.id,1)"
              v-if="has('adminRechargeLogs/index') && scope.row.type == 2 && scope.row.recharge_order_id > 0 ">
              赠送
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin:20px 0;"
        :current-page="searchParams.page"
        :page-sizes="pageSizes"
        layout="total,prev, pager, next,sizes"
        :total="data.total"
        @size-change="changeSize"
        @current-change="changePage"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import api from "../../api/api.js";
export default {
  name: "contentCate",
  data() {
    return {
      status: "index", // 页面控制
      data: [], // 表格诗句
      table_loading: true, // 表格加载中
      loading: false, //全局加载中
      pageSizes: [10, 20, 50, 100, 200, 500, 800, 1000, 2000], // 每页条数默认10
      searchParams: {
        // 搜索参数
        page: 1,
        limit: 10,
        format: "",
        type: "",
        userId: "",
        startTime: "",
        endTime: ""
      },
      formatData: [
        { id: 1, title: "金币" },
        { id: 2, title: "代充" },
        { id: 3, title: "钻石" },
        { id: 4, title: "优惠券" },
        { id: 5, title: "反悔卡" }
      ],
      typeData: [
        { id: 1, title: "充值" },
        { id: 2, title: "扣除" }
      ]
    };
  },
  created() {
    this.getData();
  },

  methods: {
    // 加载中
    load() {
      this.loading = this.$loading({
        lock: true,
        text: "数据加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
    },
    // 取消加载
    unload() {
      this.loading.close();
    },
    // 获取所有数据
    getData() {
      this.table_loading = true;
      api.adminRechargeLogs
        .index({ params: this.searchParams })
        .then(res => {
          if (res.data.code == 200) {
            this.table_loading = false;
            this.data = res.data.data;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    // 重置搜索参数重新搜索
    reset() {
      this.init();
      this.getData();
    },
    // 初始化
    init() {
      this.searchParams.page = 1;
      this.searchParams.type = "";
      this.searchParams.format = "";
      this.searchParams.startTime = "";
      this.searchParams.endTime = "";
    },
    // 改变每页条数时
    changeSize(limit) {
      this.searchParams.limit = limit;
      this.getData();
    },
    //改变状态
    cancel(id, index) {
      this.$confirm("此操作将永久改变该订单状态, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        type: "warning"
      })
        .then(() => {
          this.load();
          api.adminRechargeLogs
            .updateStatus(id)
            .then(res => {
              this.unload();
              if (res.data.code === 200) {
                this.$message({
                  type: "success",
                  message: "改变成功!"
                });
              }
            })
            .catch(error => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 改变当前页码时
    changePage(page) {
      this.searchParams.page = page;
      this.getData();
    }
  }
};
</script>

<style scoped lang="less">
.contentCate {
  margin: 0 auto;
  .search {
    margin: 0 auto 10px auto;
  }
}
</style>
